import numpy as np
import os
import json
import csv
import random
from pymongo import MongoClient

client = MongoClient(port=27017)
db=client['tiktok']
htlist=['wip', 'haventseen', 'ootd', 'bekind', 'personalfinance', 'cozyathome', 'RoomTour', 'theatrekids', 'ImAGhost', 'holidaytiktok', 'halloweenlook', 'happyhalloween', 'welldone', 'motivationmonday', 'thinkingabout', 'nonuancenovember', 'ourtype', 'fanedit', 'needtoknow', 'cleantok', 'graphicdesign', 'readysetshop', 'holidaysourway', 'onlinedating', 'myhobby', 'tiktokfood', 'whereilive', 'myrecommendation', 'worldseries', 'animation', 'cocinando', 'easydiy', 'diceroll', 'rnbvibes', 'festivefashion', 'holidaydecor', 'nbadraft', 'halloweenishere', 'christmas2020', 'howbizarre', 'sfxmakeup', 'givingthanks']
htlist=['wip', 'haventseen', 'ootd', 'bekind', 'personalfinance', 'cozyathome', 'RoomTour', 'theatrekids', 'ImAGhost', 'holidaytiktok', 'halloweenlook', 'happyhalloween', 'welldone', 'motivationmonday', 'thinkingabout', 'nonuancenovember', 'ourtype', 'fanedit', 'needtoknow', 'cleantok', 'graphicdesign', 'readysetshop', 'holidaysourway', 'onlinedating', 'myhobby', 'tiktokfood', 'whereilive', 'myrecommendation', 'worldseries', 'animation', 'cocinando', 'easydiy', 'diceroll', 'rnbvibes', 'festivefashion', 'holidaydecor', 'nbadraft', 'halloweenishere', 'christmas2020', 'howbizarre', 'sfxmakeup', 'givingthanks', 'holidayvibes', 'gamenight', 'happyholidays', 'carsoftiktok', 'fallguysmoments', 'interiordesign', 'homecooked', 'veteransday', 'youwantmore', 'coldweather', 'wildanimals', 'mycostume', 'meleaving', 'mypfp', 'catchphrases', 'watchmegrow', 'holidaycrafts', 'growupwithme', 'clingypet', 'happyhanukkah', 'lunarnewyear', 'tabletop', 'comfortfood', 'selfimprovement', '2021affirmations', 'perfectmatch', 'givingszn', 'holidaycountdown', 'bakingszn', 'holidaymusic', 'familyimpression', 'inkdrawing', 'WeekendVibes', 'recordsday', 'productivity', 'smallbusiness', 'falldiy', 'whenwewereyounger', 'yellow', 'ComingOfAge', 'artmas', 'gaminglife', 'gamingsetup', 'hellowinter', 'planttiktok', 'housetour', 'neonshadow', 'homeoffice', 'raisedby', 'makeitvogue', 'foodtiktok', 'valentinesday', 'yougotthis', 'stemlife']

ids={}
for ht in htlist:
    dayc = 0
    ids[ht] = {}
    for file in os.listdir('D:\\Work\\Tool\\tiktok\\TikToks\\'):
        if not file.endswith('.json'):
            continue
        if ht == file.split('_')[1]:
            dt = file.split('_')[2].replace('.json', '')
            tids = []
            if dayc >= 14:
                break
            with open('D:\\Work\\Tool\\tiktok\\TikToks\\' + file, 'r', encoding='utf-8',
                      newline='\n') as filename_input:
                lc = 0
                for line in filename_input:
                    z = json.loads(line)
                    if lc >= 150:
                        break
                    k = ''
                    if 'id' in z.keys():
                        k = z['id']
                    elif 'itemInfos' in z.keys():
                        k = z['itemInfos']['id']
                    if k not in tids:
                        tids.append(k)
                    lc += 1
            dayc += 1
            ids[ht][dt] = tids[:100]

with open('D:\\Work\\kusuri\\first100ids.tsv','w',newline='\n',encoding='utf-8')as fin:
    writer = csv.writer(fin, delimiter='\t')
    for ht in ids.keys():
        for dt in ids[ht].keys():
            temp=[ht,dt]
            tempids=ids[ht][dt]
            fids=[]
            for oid in tempids:
                flag=False

                for obj in db[ht].find({'_id': oid}):
                    if len(obj['video_feature']['img_embed']) > 0 and len(obj['text_feature']['text']) > 0 and len(
                            obj['video_feature']['audio']['yamnet']) > 0 and (
                            'var_sb' in obj['video_feature']['editing'].keys()) and (
                            'avg_sticker_length' in obj['video_feature']['editing'].keys()) and (
                            'avg_scences' in obj['video_feature']['editing'].keys()) and (
                            'var_yamnet' in obj['video_feature']['editing'].keys()):
                        flag=True
                if flag:
                   fids.append(oid)
            temp.extend(fids)
            writer.writerow(temp)